<template>
    <div class="play">
      <div class="header">
        <div class="title">
          <router-link to="/">
            <i class="iconfont icon-shouye left"></i>
          </router-link>
          <div class="music-info">
            <p>{{ $route.params.name }}</p>
          </div>
          <router-link to="/search">
            <i class="iconfont icon-sousuo right"></i>
          </router-link>
        </div>
      </div>
      <div class="song-info">
        <div class="song-info-img">
          <img :src="getUrl" alt />
        </div>
        <div class="iconbox">
          <i class="iconfont icon-shoucang2 left"></i>
          <i class="box"></i>
          <i class="iconfont icon-xiazai right"></i>
        </div>
      </div>
      <div class="song">
        <LRC :id="$route.params.id" :currentTime="currentTime"/>
        <!-- 使用 v-if 条件渲染 -->
        <audio v-if="playerData.url" ref="player" controls :src="playerData.url"></audio>
        <p v-else class="no-data">应用S23-5班24贾认真——没有找到对应的音乐数据</p>
      </div>
    </div>
  </template>
  
  <script>
  import LRC from "./LRC";
  
  export default {
    data() {
      return {
        playerData: { url: '' }, // 设置默认值
        currentTime: 0,
        error: null // 用于存储错误信息
      };
    },
    components: {
      LRC
    },
    mounted() {
      const id = this.$route.params.id;
      console.log('应用S23-5班24贾认真获取该歌曲ID:', id); // 调试输出
      this.$api.getPlayer({
        id: id
      })
      .then(res => {
        console.log('应用S23-5班24贾认真获取播放器数据:', res); // 调试输出
        if (res.data && res.data.length > 0) {
          this.playerData = res.data[0];
          this.addEventHandle();
        } else {
          this.error = `No data found for the given ID: ${id}`;
        //   console.error(this.error);
          this.playerData = { url: '' }; // 重置 playerData
        }
      })
      .catch(error => {
        this.error = `Error fetching player data: ${error.message}`;
        console.error(this.error);
      });
    },
    computed: {
      getUrl() {
        return decodeURIComponent(this.$route.params.image);
      }
    },
    beforeDestroy() {
      this.removeEventHandle();
    },
    methods: {
      addEventHandle() {
        if (this.$refs.player) {
          this.$refs.player.addEventListener("timeupdate", this.currentTimeHandle);
        }
      },
      removeEventHandle() {
        if (this.$refs.player) {
          this.$refs.player.removeEventListener("timeupdate", this.currentTimeHandle);
        }
      },
      currentTimeHandle() {
        this.currentTime = this.$refs.player.currentTime;
      }
    }
  };
  </script>
  
  <style scoped>
  .header {
    padding: 15px;
  }
  
  .music-info {
    flex: 1;
    font-size: 20px;
  }
  
  .title {
    display: flex;
    text-align: center;
  }
  
  .left {
    font-size: 30px;
  }
  
  .ca {
    color: red;
  }
  
  .right {
    font-size: 30px;
  }
  
  .song-info {
    padding: 15px;
  }
  
  .song-info-img {
    text-align: center;
  }
  
  .song-info-img img {
    width: 50%;
    border-radius: 5px;
    box-shadow: 0 0 10px 0 rgba(50, 50, 50, 0.31);
  }
  
  .song-lrc {
    margin-top: 10px;
    min-height: 50px;
  }
  
  .iconbox {
    display: flex;
    margin-top: 30px;
  }
  
  .iconbox .box {
    flex: 1;
  }
  
  .song {
    width: 100%;
    text-align: center;
    display: block;
  }
  
  .song audio {
    width: 80%;
  }
  
  .active {
    color: #222;
  }
  
  .author {
    font-size: 12px;
    color: #999;
  }
  
  /* 添加样式以显示友好的提示信息 */
  .no-data {
    color: red;
    font-size: 16px;
    text-align: center;
    margin-top: 20px;
  }
  </style>